/su单线程读写性能测试

import <iostream>;
import <vector>;
import <thread>;
import <chrono>;
import <atomic>;
import <mutex>;
import T_DICT; // 假设你的模块文件名为这个

using namespace std;
using namespace std::chrono;
using namespace DICT;

const int TOTAL_OPERATIONS = 100000;

int main() {
    try {
        // 初始化数据库 - 关闭不必要的功能以提高性能
        Dict db(
            "su", "performance_test_db", "root",
            1000, 100, 100, 100,
            true,  // 关闭日志
            true,  // 关闭错误日志
            true,  // 关闭控制台输出
            false,  // 关闭自动保存日志
            "",
            false,  // 关闭回收站
            false   // 关闭操作计数
        );

        // 解锁操作
        db.unlock_operation("su");

        cout << "Database initialized successfully." << endl;

        // 创建测试路径和初始值（可以在这里手动调整深度）
        vector<string> test_dir;
        test_dir.push_back("root");
        db.add("su", "su", Dict::add_type::dict, 0, test_dir, false);
        test_dir.push_back("node1");
        db.add("su", "su", Dict::add_type::dict, 0, test_dir, false);
        test_dir.push_back("node2");
        db.add("su", "su", Dict::add_type::dict, 0, test_dir, false);
        test_dir.push_back("node3");
        db.add("su", "su", Dict::add_type::dict, 0, test_dir, false);
        test_dir.push_back("node4");
        db.add("su", "su", Dict::add_type::dict, 0, test_dir, false);
        test_dir.push_back("node5");
        db.add("su", "su", Dict::add_type::dict, 0, test_dir, false);
        test_dir.push_back("node6");
        db.add("su", "su", Dict::add_type::dict, 0, test_dir, false);
        test_dir.push_back("node7");
        db.add("su", "su", Dict::add_type::dict, 0, test_dir, false);
        test_dir.push_back("node8");
        db.add("su", "su", Dict::add_type::dict, 0, test_dir, false);
        test_dir.push_back("value");
        db.add("su", "su", Dict::add_type::value, 0, test_dir, false);

        cout << "Test environment set up successfully." << endl;

        // 测试写性能
        auto write_start = high_resolution_clock::now();
        for (int i = 0; i < TOTAL_OPERATIONS; i++) {
            db.set("su", "su", Dict::set_type::value, i, test_dir, false);
        }
        auto write_end = high_resolution_clock::now();
        auto write_duration = duration_cast<milliseconds>(write_end - write_start);

        // 测试读性能
        auto read_start = high_resolution_clock::now();
        for (int i = 0; i < TOTAL_OPERATIONS; i++) {
            auto value = db.get_value("su", "su", test_dir);
        }
        auto read_end = high_resolution_clock::now();
        auto read_duration = duration_cast<milliseconds>(read_end - read_start);

        // 验证最终值
        int final_value = any_cast<int>(db.get_value("su", "su", test_dir));

        // 输出结果
        cout << "\n=== Performance Test Results ===" << endl;
        cout << "Write " << TOTAL_OPERATIONS << " operations: "
            << write_duration.count() << "ms" << endl;
        cout << "Read " << TOTAL_OPERATIONS << " operations: "
            << read_duration.count() << "ms" << endl;
        cout << "Final value: " << final_value << " (expected: " << (TOTAL_OPERATIONS - 1) << ")" << endl;

        cout << "Write operations per second: "
            << (TOTAL_OPERATIONS / (write_duration.count() / 1000.0)) << endl;
        cout << "Read operations per second: "
            << (TOTAL_OPERATIONS / (read_duration.count() / 1000.0)) << endl;

        if (final_value == TOTAL_OPERATIONS - 1) {
            cout << "Data consistency check PASSED" << endl;
        }
        else {
            cout << "Data consistency check FAILED" << endl;
        }

    }
    catch (const exception& e) {
        cerr << "Error: " << e.what() << endl;
        return 1;
    }

    while (true) {
        this_thread::sleep_for(seconds(1));
    }

    return 0;
}